package com.xxx.seckill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxx.seckill.pojo.TSeckillOrder;
import com.xxx.seckill.mapper.TSeckillOrderMapper;
import com.xxx.seckill.pojo.TUser;
import com.xxx.seckill.service.TSeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XXX
 * @since 2023-01-04
 */
@Service
public class TSeckillOrderServiceImpl extends ServiceImpl<TSeckillOrderMapper, TSeckillOrder> implements TSeckillOrderService {

    @Autowired
    private RedisTemplate redisTemplate;
    

    /**
     * 获得秒杀结果
     *
     * @param user
     * @param goodsId
     * @return orderId:成功，1，秒杀失败，0，排队中
     */
    @Override
    public Long getResult(TUser user, Long goodsId) {
        TSeckillOrder seckillOrder = baseMapper.selectOne(new QueryWrapper<TSeckillOrder>().eq("user_id",user.getId())
                .eq("goods_id",goodsId));
        if (null != seckillOrder){
            return seckillOrder.getOrderId();
        }else if(redisTemplate.hasKey("isStockEmpty:" + goodsId)){
            return -1L;
        }else{
            return 0L;
        }
    }
}
